package com.tg.paw.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tg.paw.common.constant.AllEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * @author Rubble
 * @date 2020/10/21
 */
public class QueryUtil {



    public static <T> void buildQuery(QueryWrapper<T> queryWrapper,String filter,String sorter,Class<T> klass){

        JSON.parse("", ParserConfig.getGlobalInstance());
        if(StringUtils.isNotEmpty(filter)){
            Map<String,String> map = JSON.parseObject(filter, Map.class);
            if(!map.isEmpty()){
                for(String filed: map.keySet()){
                    String values = map.get(filed);
                    List list = JSON.parseObject(values, List.class);
                    queryWrapper.in(NameUtil.humpToLine(filed),list);
                }
            }
        }
//            sorter: {"nickName":"ascend"}    sorter: {"nickName":"descend"}
        if(StringUtils.isNotEmpty(sorter) && !JSON.parseObject(sorter, Map.class).isEmpty()){
            Map<String,String> map = JSON.parseObject(sorter, Map.class);
            for(String filed: map.keySet()){
                String value = map.get(filed);
                String sortField = NameUtil.humpToLine(filed);
                if(AllEnum.SortEnum.ASC.desc().equals(value)){
                    queryWrapper.orderByAsc(sortField);
                }else {
                    queryWrapper.orderByDesc(sortField);
                }
            }
        }else {
            try {
                final String field = "createTime";
                Field createTime = klass.getDeclaredField(field);
                if(createTime!=null){
                    queryWrapper.orderByDesc(NameUtil.humpToLine(field));
                }
            } catch (NoSuchFieldException e) {
            }
        }

    }

    public static Object writeEmptyString2Null(Object object) {
        try {
            PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(object.getClass());
            for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                Class<?> propertyType = propertyDescriptor.getPropertyType();
                if(propertyType.equals(String.class)){
                    Method readMethod = propertyDescriptor.getReadMethod();
                    String value = (String) readMethod.invoke(object);
                    if(value!=null && value.isEmpty()){
                        Method writeMethod = propertyDescriptor.getWriteMethod();
                        writeMethod.invoke(object,(String)null);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return object;
    }
}
